--- 麻将牌（记录牌面和牌的种类和对应的牌的id）
---@class RiichiMahjong.Tile : Object
---@field suit "m"|"p"|"s"|"z" @万|饼|条|字
---@field number integer @点数
---@field dora boolean @是否是宝牌
---@field id integer @对应卡牌id
---@field getCardName fun(self: RiichiMahjong.Tile): string @获得卡牌的名称
---@field getCardTrans fun(self: RiichiMahjong.Tile): string @获得卡牌的中文译名
local Tile = class("Tile")

Tile.Suits = {"m", "s", "p", "z"}

---初始化
---@param suit string 卡牌或麻将牌类型
---@param number integer
---@param dora boolean
function Tile:initialize(suit, number, dora, id)
  self.suit = suit
  self.number = number
  self.dora = dora
  self.id = id
end

---获得卡牌的名称
---@param self RiichiMahjong.Tile
---@return string
function Tile:getCardName()
  return "mahjong_" .. self.suit .. (self.dora and 0 or self.number)
end

--- 通过点数和花色获取中文名
---@param suit string|integer
---@param number integer
---@return string
local getTrans = function(suit, number, dora)
  if type(suit) == "number" then
    suit = Tile.Suits[suit]
  end
  dora = dora or false
  local name
  if suit == "z" then
    if number == 1 then
      name = "Mahjong_East"
    elseif number == 2 then
      name = "Mahjong_South"
    elseif number == 3 then
      name = "Mahjong_West"
    elseif number == 4 then
      name = "Mahjong_North"
    elseif number == 5 then
      name = "Mahjong_WhiteDragon"
    elseif number == 6 then
      name = "Mahjong_GreenDragon"
    elseif number == 7 then
      name = "Mahjong_RedDragon"
    else
      return "<font color='#BE2020'>未知字牌</font>"
    end
    return Fk:translate(name)
  end 
  if number == 1 then
    name = "One"
  elseif number == 2 then
    name = "Two"
  elseif number == 3 then
    name = "Three"
  elseif number == 4 then
    name = "Four"
  elseif number == 5 then
    name = "Five"
  elseif number == 6 then
    name = "Six"
  elseif number == 7 then
    name = "Seven"
  elseif number == 8 then
    name = "Eight"
  elseif number == 9 then
    name = "Nine"
  else
    return "<font color='#BE2020'>未知点数</font>"
  end
  name = Fk:translate("Mahjong_" .. name)
  if suit == "m" then
    name = name .. Fk:translate("Mahjong_Wan")
  elseif suit == "p" then
    name = name .. Fk:translate("Mahjong_Dot")
  elseif suit == "s" then
    if number == 1 then
      name = Fk:translate("Mahjong_One_Mahjong_Bamboo")
    end
    name = name .. Fk:translate("Mahjong_Bamboo")
  else
    return "<font color='#BE2020'>未知类型</font>"
  end

  if dora then
    name = name .. Fk:translate("Mahjong_Dora")
  end

  return name
end

---获得卡牌的中文译名
---@param self RiichiMahjong.Tile
---@return string
function Tile:getCardTrans()
  return getTrans(self.suit, self.number, self.dora)
end

---@param self RiichiMahjong.Tile
---@param tile integer|RiichiMahjong.Tile
function Tile:sameAs(tile)
  local t
  local Engine = require "packages.mahjong.riichi_mahjong.core.engine"
  if type(tile) == "number" then
    t = Engine:getTileById(tile)
  elseif type(tile) == "table" then
    t = tile
  end
  return self.suit == t.suit and self.number == t.number
end

return Tile